package com.tongtong.stsuserapi.service.base.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tongtong.stscommon.enumration.*;
import com.tongtong.stscommon.exception.STSAuditedException;
import com.tongtong.stscommon.exception.STSResourcesOperationException;
import com.tongtong.stscommon.message.STSMessage;
import com.tongtong.stscommon.message.STSSuccessMessage;
import com.tongtong.stscommon.utils.checker.ObjectChecker;
import com.tongtong.stsuserapi.dao.CollectionCodeDao;
import com.tongtong.stsuserapi.dao.UserDao;
import com.tongtong.stsuserapi.pojo.CollectionCode;
import com.tongtong.stsuserapi.pojo.User;
import com.tongtong.stsuserapi.service.base.ManagerService;
import com.tongtong.stsuserapi.service.base.NotificationService;
import com.tongtong.stsuserapi.service.base.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ManagerServiceImpl implements ManagerService {

	@Autowired
	UserService userService;

	@Autowired
	UserDao userDao;

	@Autowired
	CollectionCodeDao collectionCodeDao;

	@Autowired
	NotificationService notificationService;

	@Override
	public STSMessage DeleteUsersByList(List<Long> userIds) {

		try	{
			userDao.deleteBatchIds(userIds);
		} catch (Exception e) {
			throw new STSResourcesOperationException(Status.DeleteResourcesError,e.getMessage());
		}
		return STSSuccessMessage.from(Status.OK,Message.OK);
	}

	@Override
	public STSMessage addUser(User user, MultipartFile file) throws Exception {

		HashMap<String,String> map = (HashMap<String, String>) userService.addUser(user).getData();
		Long userid = Long.parseLong(map.get("userid"));

		return userService.updateHeadById(userid,file);
	}

	@Override
	public STSMessage alterUser(User user) {

		ObjectChecker.ObjectsFromHTTPNullCheck(user);

		try {
			userDao.updateById(user);
		} catch (Exception e) {
			throw new STSResourcesOperationException(Status.UpdateResourcesError,e.getMessage());
		}

		return STSSuccessMessage.from(Status.OK,Message.OK);
	}

	@Override
	public STSMessage getCodesToBeAudited(Integer codeState, Integer page, Integer pageSize) {
		Page<CollectionCode> pageInfo = new Page(page, pageSize);
		LambdaQueryWrapper<CollectionCode> lqw = new LambdaQueryWrapper<>();

		switch (codeState) {
			case CollectionCodeState.ToBeAudited:
				lqw.eq(CollectionCode::getCodeState, CollectionCodeState.ToBeAudited);
				lqw.orderByDesc(CollectionCode::getApplyTime);
				break;
			case CollectionCodeState.Passed:
				lqw.eq(CollectionCode::getCodeState, CollectionCodeState.Passed);
				lqw.orderByDesc(CollectionCode::getUpdateTime);
				break;
			case CollectionCodeState.Refused:
				lqw.eq(CollectionCode::getCodeState, CollectionCodeState.Refused);
				lqw.orderByDesc(CollectionCode::getUpdateTime);
				break;
			default:
				throw new STSAuditedException(Status.AuditedStatusError,Message.AuditedStatusError);
		}

		Page<CollectionCode> codePage = collectionCodeDao.selectPage(pageInfo, lqw);
		List<CollectionCode> list = codePage.getRecords();
		Long len = collectionCodeDao.selectCount(lqw);

		HashMap<String,Object> map = new HashMap<>();
		map.put("list",list);
		map.put("len",len);

		return STSSuccessMessage.from(map);
	}

	@Override
	public STSMessage codeAudit(Long codeId, Integer result, String reason) {

		CollectionCode collectionCode = collectionCodeDao.selectById(codeId);
		ObjectChecker.ObjectsFromDBNullCheck(collectionCode);

		if(collectionCode.getCodeState() != CollectionCodeState.ToBeAudited) {
			throw new STSAuditedException(Status.AuditedStatusError,Message.AuditedStatusError);
		}

		String content = null;
		switch (result) {
			case CollectionCodeState.Passed:
				collectionCode.setCodeState(CollectionCodeState.Passed);
				content = "叮咚!您的收款码已通过审核";
				break;
			case CollectionCodeState.Refused:
				collectionCode.setCodeState(CollectionCodeState.Refused);
				content = "呜呜,您的收款码审核未通过,拒绝原因: "+reason;
				break;
			default:
				throw new STSAuditedException(Status.AuditedResultError,Message.AuditedResultError);
		}

		collectionCodeDao.updateById(collectionCode);
		notificationService.sendNotification(NotificationType.SystemID,collectionCode.getUserId(),content, NotificationType.SystemNotice);

		return STSSuccessMessage.from(Status.OK,Message.OK);
	}
}
